package com.fiberhome.ztelauncher.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.resource.ResourceManager;
import com.fiberhome.ztelauncher.uilib.GlideRoundTransform;

/**
 * Created by xc on 2018/3/22.
 * 描述：
 */

public class BitmapUitl {

    private static final String ASSETS_PATH = "file:///android_asset/";

    /**
     * @param context
     * @param view
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void loadBackground(Context context, final View view, String url) {
        if(context == null){
            return;
        }
        SimpleTarget target = new SimpleTarget<Drawable>() {
			@SuppressWarnings("deprecation")
			@Override
			public void onResourceReady(Drawable drawable, GlideAnimation<? super Drawable> arg1) {
				view.setBackgroundDrawable(drawable);
			}
		};
		
//        RequestOptions options = new RequestOptions();
		String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl) 
                .into(target);
        
        
       
    }

    /**
     * @param context
     * @param view
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void loadBackgroundRound(Context context, final View view, String url) {
        if(context == null){
            return;
        }
//        RequestOptions options = new RequestOptions()
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
//                .transforms(new GlideRoundTransform(context));
		SimpleTarget target = new SimpleTarget<Drawable>() {
			@SuppressWarnings("deprecation")
			@Override
			public void onResourceReady(Drawable drawable, GlideAnimation<? super Drawable> arg1) {
				view.setBackgroundDrawable(drawable);
			}
		};
		String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .into(target);
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmap(Context context, ImageView imageView, String url) {
        if(context == null){
            return;
        }
//        RequestOptions options = new RequestOptions().centerInside();
        String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .into(imageView);
    }
    
    /**
     * @param context
     * @param imageView
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void loadBitmapTag(Context context, final ImageView imageView, String url) {
        if(context == null){
            return;
        }
        SimpleTarget target = new SimpleTarget<Drawable>() {
			@SuppressWarnings("deprecation")
			@Override
			public void onResourceReady(Drawable drawable, GlideAnimation<? super Drawable> arg1) {
				imageView.setImageDrawable(drawable);
			}
		};
		String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .into(target);
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmapRound(Context context, ImageView imageView, String url) {
        if(context == null){
            return;
        }
//        RequestOptions options = new RequestOptions()
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
//                .transforms(new GlideRoundTransform(context));
        String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transform(new GlideRoundTransform(context))
                .into(imageView);
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmapWH(Context context, ImageView imageView, String url,int width,int height) {
        if(context == null){
            return;
        }
//        RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.ic_launcher)
//                .error(R.drawable.ic_launcher)
//                .override(width,height);
        String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .placeholder(R.drawable.ic_launcher)
                .error(R.drawable.ic_launcher)
                .override(width,height)
                .into(imageView);
    }

    /**
     * @param context
     * @param tv
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void loadLeft(Context context, final TextView view, String url) {
        if(context == null){
            return;
        }
        SimpleTarget target = new SimpleTarget<Drawable>() {
			@SuppressWarnings("deprecation")
			@Override
			public void onResourceReady(Drawable drawable, GlideAnimation<? super Drawable> arg1) {
				view.setCompoundDrawables(drawable, null, null, null);
			}
		};
		String absUrl = ResourceManager.getResourceManager().getResourceUrl(url);
        Glide.with(context)
                .load(absUrl)
                .into(target);
    }
}
